<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>03绘制一个点要素</title>
    <!-- 1.导入依赖 -->
    <link rel="stylesheet" href="https://lib.baomitu.com/ol3/4.6.5/ol.css" />
    <script src="https://lib.baomitu.com/ol3/4.6.5/ol.js"></script>
  </head>
  <body>
    <!-- 2.设置地图容器的挂载点 -->
    <div id="map"></div>

    <script>
      // 3.初始化一个高德图层(初始化地图)
      const gaode = new ol.layer.Tile({
        title: '高德地图',
        source: new ol.source.XYZ({
          url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
          wrapX: false,
        }),
      })

      // 4.初始化openlayer地图
      const map = new ol.Map({
        // 将初始化的地图设置到id为map的DOM元素上
        target: 'map',
        layers: [gaode],
        view: new ol.View({
          center: [113.73, 33.62],
          // 设置地图的放大级别
          zoom: 16,
          projection: 'EPSG:4326',
        }),
      })

      // -----------04geojson创建点要素------------------
      // ①.创建GeoJSON数据
      var data = {
        type: 'FeatureCollection',
        features: [
          {
            type: 'Feature',
            geometry: {
              type: 'Point',
              coordinates: [113.73, 33.62],
            },
          },
        ],
      }
      // ②.创建矢量数据源以添加GeoJSON数据
      var source = new ol.source.Vector({
        features: new ol.format.GeoJSON().readFeatures(data),
      })
      // ③.创建矢量图层以放置矢量数据源
      var layer = new ol.layer.Vector({
        source: source,
      })
      //设置样式
      const style = new ol.style.Style({
        image: new ol.style.Circle({
          radius: 10,
          fill: new ol.style.Fill({
            color: 'red',
          }),
          stroke: new ol.style.Stroke({
            color: '#000',
          }),
        }),
      })
      layer.setStyle(style)
      //  ④.在地图容器中添加矢量图层
      map.addLayer(layer)
    </script>
  </body>
</html>
